/**
 * 
 */
package com.ihg.hiex.admin.web.view;

import java.io.File;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.view.AbstractView;

import net.sf.jxls.transformer.XLSTransformer;

/**
 * 
 * @author David
 */
public class ExcelTemplateView extends AbstractView {
	
	private static final Logger logger = LoggerFactory.getLogger(ExcelTemplateView.class); 

	/** content type for csv response = application/csv */
    /** The content type for an Excel response */
    private static final String CONTENT_TYPE = "application/vnd.ms-excel";

    private static final String CONTENT_DISPOSITION_PREFIX = "attachment;";

    private static final String DEFAULT_FILE_NAME = "download.xls";

    private String templateXLSFile;

    private String fileName;

    /**
     * @param templateXLSFile
     */
    public ExcelTemplateView(String templateXLSFile) {
        super.setContentType(CONTENT_TYPE);
        this.templateXLSFile = templateXLSFile;
        this.fileName = templateXLSFile;
    }

    /**
     * @param templateXLSFile
     * @param fileName
     */
    public ExcelTemplateView(String templateXLSFile, String fileName) {
        super.setContentType(CONTENT_TYPE);
        this.templateXLSFile = templateXLSFile;
        this.fileName = fileName;
    }
    
    /**
     * @return the fileName
     */
    public String getFileName() {
        return fileName;
    }

    /**
     * @param fileName the fileName to set
     */
    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    /**
     * 
     * @return
     */
    public String getContentDisposition() {
        String fileName = StringUtils.isBlank(this.fileName) ? DEFAULT_FILE_NAME : this.fileName;
        return CONTENT_DISPOSITION_PREFIX + " filename=" + fileName;
    }
    
    /* 
     * (non-Javadoc)
     * 
     * @see org.springframework.web.servlet.view.AbstractView#renderMergedOutputModel(java.util.Map, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {

        logger.info("Start exporting excel file: " + this.templateXLSFile);
        File template = ResourceUtils.getFile("classpath:template/" + this.templateXLSFile);

        Workbook workbook = WorkbookFactory.create(template);

        // transfer workbook
        XLSTransformer transformer = new XLSTransformer();
        transformer.transformWorkbook(workbook, model);

        response.setHeader("Content-Disposition", getContentDisposition());
        // no cache here
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");  
        response.setDateHeader("Expire", 0); 
        response.setContentType(super.getContentType());

        ServletOutputStream outputStream = response.getOutputStream();

        workbook.write(outputStream);
        outputStream.flush();
        logger.info("End exporting excel file: " + this.templateXLSFile);

    }

}
